package utils;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

import utils.getcode.GetToken;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import beans.FriendsStatuses;
import dbtools.WeiboDb;

/**
 * @author sina
 *
 */
public class GetFriends implements Runnable{
	
	//public static final Log log = LogFactory.getLog(GetFriends.class);
	private static final Logger log = Logger.getLogger(GetFriends.class);
	
	private final int  pageSize;
	
	private Weibo weibo = null;
	
	private AccessToken at = null;
	
	private String weiboId;
	
	WeiboDb db = new WeiboDb();
	
	public GetFriends(Weibo weibo,AccessToken at, String weiboId){
		this(weibo,at,100, weiboId);
	}
	
	public GetFriends(Weibo weibo,AccessToken at,int size, String weiboId){
		pageSize = size;
		this.weibo = weibo;
		this.at = at;
		this.weiboId = weiboId;
		
		this.weibo.setToken(this.at);
	}
	
	@Override
	public void run() {
		getFriends(true);
		
	}
	
	/**
	 * @param toSave
	 * true: save to DB
	 * false: not save
	 */
	public List<User> getFriends(boolean toSave) {
		
		if (weiboId == null || "".equals(weiboId)){
			return null;
		}


		try {

			int cursor = -1;
			List<User> result = new LinkedList<User>();
			FriendsStatuses fs = weibo.getFriendsStatuses2(weiboId, cursor,
					pageSize);
			List<User> list = fs.list;
			if (!toSave){
				result.addAll(list);
			}else{
				saveDB(list);
			}
			
			while (list != null && list.size() > 0) {
				if (fs.next_cursor < -1  || fs.next_cursor == 0){
					break;
				}
				fs = weibo.getFriendsStatuses2(weiboId, fs.next_cursor,
						pageSize);
				log.debug(weiboId);
				log.debug("cursor:" + fs.next_cursor);
				list = fs.list;
				if (toSave){
					saveDB(list);
				}else{
					result.addAll(list);
				}
				Thread.currentThread().sleep(1000 * 20); // wait for 10 seconds
			}
			
			return result;

		} catch (Exception e1) {
			log.debug(e1);
		}

		return null;
	}
	
	private void saveDB(List<User> list){
		if (list == null || list.size() == 0)
			return;
		
		StringBuilder friends = new StringBuilder("");
		
		for(Iterator<User> i = list.iterator();i.hasNext();){
			User u = i.next();
			friends.append("{").append(String.valueOf(u.getId())).append("}");
		}
		
		StringBuilder sql = new StringBuilder("update baseinfo set friends=");
		sql.append("concat(ifnull(friends,\"\"),\"").append(friends.toString()).append("\")");
		sql.append(" where id=\'");
		sql.append(this.weiboId);
		sql.append("\'");
		

		db.update(sql.toString());
//		db.dispose();
		
	}
	
	public static void main(String[] args) {
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
    	System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		try {
			Weibo weibo = new Weibo();
			
			GetToken gt = new GetToken(); 
			AccessToken at = gt.getAccessToken();
			if (at == null)
				return;
			
			GetFriends gf = new GetFriends(weibo,at,"1775691671");
			
			List<User> list = gf.getFriends(true);
//
//			System.out.println(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
